package com.sanyog.d8mb;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.mail.MessagingException;

import org.primefaces.context.RequestContext;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.event.RateEvent;

import com.sanyog.util.DBImpl;
import com.sanyog.util.GoogleTest;

@ManagedBean
@SessionScoped
public class BlogBean implements Serializable {

	/**
	 * Sanyog Barve
	 */  
	private static final long serialVersionUID = 1L;

	public static String newline = System.getProperty("line.separator");
	private String name;
	private String password;
	private String email;
	private String message;
	private String text;
	public String city;
	public String locality;
	public String category;
	public String title;
	public String description;
	public String image1;
	public String image2;
	public String mobile;
	public String rating;
	
	
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRating() {
		return rating;
	}

	public void setRating(String rating) {
		this.rating = rating;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}  
    
    public String getText() {  
        return text;  
    }  
    public void setText(String text) {  
        this.text = text;  
    }  
    
    public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getLocality() {
		return locality;
	}

	public void setLocality(String locality) {
		this.locality = locality;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getImage1() {
		return image1;
	}

	public void setImage1(String image1) {
		this.image1 = image1;
	}

	public String getImage2() {
		return image2;
	}

	public void setImage2(String image2) {
		this.image2 = image2;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public void save(ActionEvent actionEvent) throws MessagingException  {  
        //FacesContext context = FacesContext.getCurrentInstance();  
    	RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;
		boolean loggedIn = false;
		String subject = "Contact Us Mail !!!";
		
		if(name != null  && email != null  && message != null) {
			loggedIn = true;
			GoogleTest gt = new GoogleTest();
			gt.sendSSLMessage(newline+"Visitor's Name ::"+name+newline+"Visitor's E-Mail ::"+email+newline+"Visitor's Message ::"+message,subject);
	        msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", "Your Mail Sent Successfully");
	        setEmail("");
	        setName("");
	        setMessage("");
		} else {
			loggedIn = false;
			msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Error", "Fields cannot be empty");
		}
		
		FacesContext.getCurrentInstance().addMessage(null, msg);
		context.addCallbackParam("loggedIn", loggedIn);
		
		
        /*System.out.println("1");
        if(name.equals("") || message.equals("") || email.equals("")){
        	System.out.println("2");
        	context.addMessage(null, new FacesMessage("Error", "Fields cannot be empty"));
        }
        else{
        	System.out.println("3");
	        GoogleTest gt = new GoogleTest();
			gt.sendSSLMessage(newline+"Visitor's Name ::"+name+newline+"Visitor's E-Mail ::"+email+newline+"Visitor's Message ::"+message);
	        
	        context.addMessage(null, new FacesMessage("Successful", "Your Mail Sent Successfully"));
        }*/
    }
	
	public void handleFileUpload(FileUploadEvent event) {  
		System.out.println("file Upload");
        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }  
	
	public void postAd(ActionEvent actionEvent) throws MessagingException  {  
		System.out.println("postAd");
        //FacesContext context = FacesContext.getCurrentInstance();  
    	RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;
		boolean loggedIn = false;
		
		
		if(email != null) {
			loggedIn = true;
		    msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", "Your Ad Posted Successfully");
	    } else {
	    	System.out.println("Fields Empty");
			loggedIn = false;
			msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Error", "Fields cannot be empty");
		}
		
		FacesContext.getCurrentInstance().addMessage(null, msg);
		context.addCallbackParam("loggedIn", loggedIn);
	} 
	
	 public void onrate(RateEvent rateEvent) throws MessagingException {  
	        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Hurray !!!", "You rated:" + rateEvent.getRating().toString());  
	        FacesContext.getCurrentInstance().addMessage(null, message);  
	    }  
	      
	    public void oncancel() {  
	        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Cancel Event", "Rate Reset");  
	  
	        FacesContext.getCurrentInstance().addMessage(null, message);  
	    }  
	    
	    public void rateus(ActionEvent actionEvent) throws MessagingException  {  
	        //FacesContext context = FacesContext.getCurrentInstance();  
	    	RequestContext context = RequestContext.getCurrentInstance();
	        FacesMessage msg = null;
			boolean loggedIn = false;
			String subject = "Rate Us Mail...";
			
			if(name != null  && email != null) {
				System.out.println(name+email+rating);
				loggedIn = true;
				GoogleTest gt = new GoogleTest();
				gt.sendSSLMessage(newline+"Visitor's Name ::"+name+newline+"Visitor's E-Mail ::"+email+newline+"Visitor's rating ::"+rating,subject);
				//DBImpl impl = new DBImpl();
				//impl.addRating(name, email, rating);
		        msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", "Thank you for rating us.");
		    } else {
				loggedIn = false;
				msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Error", "Fields cannot be empty");
			}
			
			FacesContext.getCurrentInstance().addMessage(null, msg);
			context.addCallbackParam("loggedIn", loggedIn);
		}
	    
	    public void join(ActionEvent actionEvent) throws MessagingException  {  
	        //FacesContext context = FacesContext.getCurrentInstance();  
	    	RequestContext context = RequestContext.getCurrentInstance();
	        FacesMessage msg = null;
			boolean loggedIn = false;
			String subject = "Joining Mail...";
			
			if(name != null  && email != null) {
				System.out.println(name+email);
				loggedIn = true;
				GoogleTest gt = new GoogleTest();
				gt.sendSSLMessage(newline+"Visitor's Name ::"+name+newline+"Visitor's E-Mail ::"+email,subject);
				//DBImpl impl = new DBImpl();
				//impl.addRating(name, email, rating);
		        msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", "Thank you for joining d8mb.");
		    } else {
				loggedIn = false;
				msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Error", "Fields cannot be empty");
			}
			
			FacesContext.getCurrentInstance().addMessage(null, msg);
			context.addCallbackParam("loggedIn", loggedIn);
		}
	    
	    public void login(ActionEvent actionEvent) {
			RequestContext context = RequestContext.getCurrentInstance();
			FacesMessage msg = null;
			boolean loggedIn = false;
			
			if(name != null  && name.equals("admin") && password != null  && password.equals("admin")) {
				loggedIn = true;
				msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", name);
			} else {
				loggedIn = false;
				msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "Invalid credentials");
			}
			
			FacesContext.getCurrentInstance().addMessage(null, msg);
			context.addCallbackParam("loggedIn", loggedIn);
		}
}